/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.web.page;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.annotations.InjectState;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.annotations.Bean;
import org.apache.tapestry.valid.ValidationDelegate;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.IPage;
import org.apache.tapestry.html.BasePage;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.event.PageBeginRenderListener;
import com.javaforge.tapestry.spring.annotations.InjectSpring;
import com.crm.service.IProcessService;
import com.crm.service.IUserService;
import com.crm.model.User;

import java.util.List;
import java.util.ArrayList;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public abstract class SysUser extends BasePage implements PageBeginRenderListener {

    private static Log logger = LogFactory.getLog(UserList.class);
    private int count;

    @InjectSpring("processService")
    public abstract IProcessService getProcessService();

    @InjectSpring("userService")
    public abstract IUserService getUserServ();

    @InjectState("userSession")
    public abstract User getUserSession();

    @InjectPage("SysUserNew")
    public abstract SysUserNew getSysUserNewPage();

    @Bean
    public abstract ValidationDelegate getDelegate();

    public IPage onShowDetails(User user) {
        logger.info(getUserSession().getUsername() + ": check user details by id - " + user.getId());
        SysUserNew su = getSysUserNewPage();
        su.setEditUser(user);
        return getSysUserNewPage();
    }

    public void pageBeginRender(PageEvent event) {
        if (getUserSession().getUsername() == null || !getUserSession().getUserGroup().equals(User.GROUP_SUPER)) {
            event.getRequestCycle().sendRedirect("app?page=SysPageError&service=page");
        }
    }

    public String getRecordCount() {
        return "共" + count + "名用户";
    }

    public List getEntries() {
        try {
            List list = getUserServ().getUsers();
            count = list.size();
            return list;
        }
        catch (Exception e) {
            String exceptionString = "The exception happened when list user information. " + e.getMessage();
            logger.error(exceptionString);
            e.printStackTrace();
            getDelegate().record(exceptionString, ValidationConstraint.CONSISTENCY);
            return new ArrayList();
        }
    }

    public String newUser() {
        getSysUserNewPage().setEditUser(null);
        return "SysUserNew";
    }


    public void deleteUser(User u) {
        try {
            getUserServ().deleteUser(u);
            getDelegate().record("用户【" + u.getUsername() + "】已删除", ValidationConstraint.CONSISTENCY);
            logger.info(getUserSession().getUsername() + ": Deleted a user [" + u.getTruename() + "].");
        } catch (Exception e) {
            String s = "无法删除用户，可能系统还存在与此用户相关的客户资料未清除。";
            logger.error(s + e);
            e.printStackTrace();
            getDelegate().record(s, ValidationConstraint.CONSISTENCY);
        }
    }

}
